import React from 'react';
import type { BizAlarmRuleInfo } from '@/api/rulesWarning';
import { AlarmLevelEnum, AlarmTypeEnum, objToArray } from '@/constants';

// 将枚举对象转换为Select组件需要的选项数组
const levelOptions = objToArray(AlarmLevelEnum);
const typeOptions = objToArray(AlarmTypeEnum);

interface WarningAddFormProps {
  form: FormInstance;
  data?: Partial<BizAlarmRuleInfo>;
  mode: 'add' | 'edit';
}

/**
 * 告警规则添加/编辑表单组件
 */
const WarningAddForm: React.FC<WarningAddFormProps> = ({ form, data, mode }) => {
  const isEdit = mode === 'edit';

  return (
    <>
      <AForm.Item
        name="alarmRuleName"
        label="规则名称"
        rules={[{ required: true, message: '请输入告警规则名称' }]}
      >
        <AInput placeholder="请输入告警规则名称" maxLength={50} />
      </AForm.Item>

      <AForm.Item
        name="level"
        label="告警级别"
        rules={[{ required: true, message: '请选择告警级别' }]}
      >
        <ASelect placeholder="请选择告警级别" options={levelOptions} allowClear={false} />
      </AForm.Item>

      <AForm.Item
        name="type"
        label="告警类型"
        rules={[{ required: true, message: '请选择告警类型' }]}
      >
        <ASelect
          placeholder="请选择告警类型"
          options={typeOptions}
          allowClear={false}
          disabled={isEdit} // 编辑模式下不允许修改告警类型
        />
      </AForm.Item>

      <AForm.Item name="remark" label="规则描述" rules={[{ required: false }]}>
        <AInput.TextArea placeholder="请输入规则描述" maxLength={200} showCount rows={4} />
      </AForm.Item>

      {/* 隐藏字段，用于编辑模式 */}
      {isEdit && (
        <AForm.Item name="id" hidden>
          <AInput />
        </AForm.Item>
      )}
    </>
  );
};

export default WarningAddForm;
